package com.example.demo.hutool.redis;

import com.example.demo.hutool.BitMap;
import org.openjdk.jol.info.GraphLayout;
import redis.clients.jedis.Jedis;

import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class RedisCache {

    public BitMap stringBitMap;
    public BitMap hashBitMap;
    public BitMap listBitMap;
    public BitMap setBitMap;
    public BitMap zsetBitMap;

    public static void main(String[] args) throws NoSuchAlgorithmException {


        BitMap bitMap = new BitMap(1999999999);

        BitMap string = new BitMap(1999999999);
        BitMap hashMap = new BitMap(1999999999);
        BitMap list = new BitMap(1999999999);
        BitMap set = new BitMap(1999999999);
        BitMap zset = new BitMap(1999999999);
        HashSet<Integer> strings = new HashSet<>();

        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        Jedis resource = RedisTypeTest.getJedisPool().getResource();
        int count=0;
        Set<String> keys = resource.keys("*");
        for (String key : keys) {
            int hash = hash(key);
            if (strings.contains(hash)){
                count++;
                objectObjectHashMap.put(hash+"",key);
                System.out.println("重复"+count);
            }
            strings.add(hash);
        }
        System.out.println();

    }

    static final int hash(String key) {
        long index = Math.abs((long) (key.hashCode() % 1999999999));
        int length = key.length();
        if (index - length > 1) {
           // index = index - length;
        }
        return (int) index;
    }


    public static long changeByte2LongIndex(String url) throws NoSuchAlgorithmException {
        byte[] bytes = MessageDigest.getInstance("MD5").digest(url.getBytes());
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        long abs = (long) Math.abs(buffer.getLong() % 1999999999);

        return abs;
    }

    static final long[] byteTable = createLookupTable();
    static final long HSTART = 0xBB40E64DA205B064L;
    static final long HMULT = 7664345821815920749L;

    private static final long[] createLookupTable() {
        long[] byteTable = new long[256];
        long h = 0x544B2FBACAAF1684L;
        for (int i = 0; i < 256; i++) {
            for (int j = 0; j < 31; j++) {
                h = (h >>> 7) ^ h;
                h = (h << 11) ^ h;
                h = (h >>> 10) ^ h;
            }
            byteTable[i] = h;
        }
        return byteTable;
    }

    public static long hashCode(CharSequence cs) {
        long h = HSTART;
        final long hmult = HMULT;
        final long[] ht = byteTable;
        final int len = cs.length();
        for (int i = 0; i < len; i++) {
            char ch = cs.charAt(i);
            h = (h * hmult) ^ ht[ch & 0xff];
            h = (h * hmult) ^ ht[(ch >>> 8) & 0xff];
        }
        return h;
    }


}
